package com.ruoyi.zxjd.document.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.SmsUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.zxjd.common.CommonConstants;
import com.ruoyi.zxjd.document.domain.DocumentProgress;
import com.ruoyi.zxjd.document.mapper.DocumentProgressMapper;
import com.ruoyi.zxjd.document.service.IDocumentProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 公文处理Service业务层处理
 * 
 * @author lcc
 * @date 2021-01-28
 */
@Service
public class DocumentProgressServiceImpl implements IDocumentProgressService 
{
    @Autowired
    private DocumentProgressMapper documentProgressMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询公文处理
     * 
     * @param progressId 公文处理ID
     * @return 公文处理
     */
    @Override
    public DocumentProgress selectDocumentProgressById(Long progressId)
    {
        return documentProgressMapper.selectDocumentProgressById(progressId);
    }

    /**
     * 查询公文处理列表
     * 
     * @param documentProgress 公文处理
     * @return 公文处理
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<DocumentProgress> selectDocumentProgressList(DocumentProgress documentProgress)
    {
        return documentProgressMapper.selectDocumentProgressList(documentProgress);
    }

    /**
     * 新增公文处理
     * 
     * @param documentProgress 公文处理
     * @return 结果
     */
    @Override
    public int insertDocumentProgress(DocumentProgress documentProgress)
    {
        documentProgress.setCreateBy(ShiroUtils.getLoginName());
        documentProgress.setCreateTime(DateUtils.getNowDate());
        documentProgress.setStatus(CommonConstants.TO_BE_RECEIVED);
        return documentProgressMapper.insertDocumentProgress(documentProgress);
    }

    /**
     * 修改公文处理
     * 
     * @param documentProgress 公文处理
     * @return 结果
     */
    @Override
    public int updateDocumentProgress(DocumentProgress documentProgress)
    {
        documentProgress.setUpdateBy(ShiroUtils.getLoginName());
        documentProgress.setUpdateTime(DateUtils.getNowDate());
        return documentProgressMapper.updateDocumentProgress(documentProgress);
    }

    /**
     * 删除公文处理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDocumentProgressByIds(String ids)
    {
        return documentProgressMapper.deleteDocumentProgressByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除公文处理信息
     * 
     * @param progressId 公文处理ID
     * @return 结果
     */
    @Override
    public int deleteDocumentProgressById(Long progressId)
    {
        return documentProgressMapper.deleteDocumentProgressById(progressId);
    }

    /**
     * 接收公文处理
     * @param documentProgress
     * @return
     */
    @Override
	public int receive(DocumentProgress documentProgress)
    {   
        Long progressId = documentProgress.getProgressId();
        String status = CommonConstants.RECEIVED;
        String nowDate = DateUtils.getDate();
        String endDate = "";
        DocumentProgress oldDocumentProgress = documentProgressMapper.selectDocumentProgressById(progressId);
        if(oldDocumentProgress.getEndDate()!=null){
            endDate = DateUtils.parseDateToStr("yyyy-MM-dd", oldDocumentProgress.getEndDate());
        }
        if (StringUtils.compare(nowDate, endDate)>0) {
            status = CommonConstants.OVERDUE_RECEIVED;
        }
        documentProgress.setStatus(status);
        documentProgress.setReceiveDate(DateUtils.getNowDate());
        return updateDocumentProgress(documentProgress);
    }

    @Override
    public int smsPrompt(Long progressId) {

        DocumentProgress documentProgress = documentProgressMapper.selectDocumentProgressById(progressId);
        SysUser user = sysUserMapper.selectUserById(documentProgress.getReceiveUserId());
        String message = user.getUserName()+"，您好！在线监督平台提示: 您有一项公文(" + documentProgress.getArrangeTitle() + ")待接收";
        String mobileNo = user.getPhonenumber();
//        mobileNo = "13281028987";
        SmsUtil.sendMessage(message, mobileNo);
        return 1;
    }

}
